package com.chuantou.yuba.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.ConversationAdapter;
import com.chuantou.yuba.util.ActivityHelper;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.ChatUtils;
import com.chuantou.yuba.util.Constants;
import com.easemob.applib.utils.SmileUtils;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.DateUtils;

public class FollowMsgActivity extends BaseActionBarActivity {
	private String mHxuid;
	private ListView mListView;
	private EMConversation mConversation;
	private MessageAdapter mMessageAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.follow_msg);
		mHxuid = getIntent().getStringExtra(ChatActivity.EXTRA_UID);
		setupViews();
		mConversation = EMChatManager.getInstance().getConversation(mHxuid);
		if (mConversation != null) {
			List<EMMessage> list = mConversation.getAllMessages();
			mConversation.resetUnreadMsgCount();
			mMessageAdapter.add(list);
			mMessageAdapter.notifyDataSetChanged();
		}
		registerForContextMenu(mListView);
		setTitle(R.string.new_friend);
	}

	private void setupViews() {
		mListView = (ListView) findViewById(R.id.list);
		mMessageAdapter = new MessageAdapter(getApplicationContext());
		mListView.setAdapter(mMessageAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				EMMessage message = (EMMessage) parent.getItemAtPosition(position);
				long uid = 0;
				try {
					String uidStr = message.getStringAttribute(Constants.ATTR_FOLLOWER_ID, "0");
					uid = Long.parseLong(uidStr);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (uid > 0) {
					ActivityHelper.toProfileActivity(FollowMsgActivity.this, uid);
				}
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.menu_delete, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_delete) {
			EMMessage message = (EMMessage) mMessageAdapter.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position);
			mConversation.removeMessage(message.getMsgId());
			mMessageAdapter.removeMessage(message);
			mMessageAdapter.notifyDataSetChanged();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	private class MessageAdapter extends BaseAdapter {
		private List<EMMessage> mList = new ArrayList<EMMessage>();
		private Context mContext;
		private LayoutInflater mInInflater;

		public MessageAdapter(Context context) {
			this.mContext = context;
			mInInflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		public void removeMessage(EMMessage message) {
			mList.remove(message);
		}

		public void add(List<EMMessage> messages) {
			if (messages != null && !messages.isEmpty()) {
				mList.addAll(messages);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInInflater.inflate(R.layout.follow_msg_item, parent, false);
			}
			TextView nameView = (TextView) convertView.findViewById(R.id.name);
			TextView messageView = (TextView) convertView.findViewById(R.id.message);
			TextView timeView = (TextView) convertView.findViewById(R.id.time);
			NetworkImageView avatarView = (NetworkImageView) convertView.findViewById(R.id.avatar);
			// 获取与此用户/群组的会话
			EMMessage message = mList.get(position);
			String username = null;
			try {
				username = ChatUtils.getFollower(message);
			} catch (EaseMobException e) {
				e.printStackTrace();
			}
			String avatar = null;
			try {
				avatar = ChatUtils.getFollowerAvatar(message);
			} catch (EaseMobException e) {
				e.printStackTrace();
			}
			BitmapBinder.bindCircularNetImage(mContext, avatarView, avatar, R.drawable.default_avatar_round_small);
			// 获取用户username或者群组groupid
			nameView.setText(username);
			messageView.setText(SmileUtils.getSmiledText(mContext, ConversationAdapter.getMessageDigest(message, mContext)), BufferType.SPANNABLE);
			timeView.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));

			return convertView;
		}
	}
}
